package com.xinruke.hostels.systemmanage.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.cache.extract.extractor.DictInfoCacheExtractor;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwHuichangleixing;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.system.manage.interfaces.dto.*;
import com.xinruke.hostels.systemmanage.mapper.YwHuichangleixingMapper;
import com.xinruke.hostels.systemmanage.service.MeetingTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yutaipu
 */
@Service
public class MeetingTypeServiceImpl implements MeetingTypeService {

    @Autowired
    private YwHuichangleixingMapper ywHuichangleixingMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Resource
    private DictInfoCacheExtractor dictInfoCacheExtractor;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO add(MeetingTypeAddDTO addQueryDTO) {

        ResultVO resultVO = ResultVO.success();


        LoginManageResultVO userInfo = manageUserCache.getLoginManageUserInfo();

        YwHuichangleixing add = new YwHuichangleixing();
        BeanUtils.copyProperties(addQueryDTO, add);
        add.setJiudianId(userInfo.getJiudianId());
        add.setChuangjianrenId(userInfo.getId());
        add.setChuangjianshijian(new Date());
        add.setGengxinrenId(userInfo.getId());
        add.setGengxinshijian(new Date());
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        add.setZhuangtai(SysDicConstant.ZHUANGTAI_YOU_XIAO);
        int count = ywHuichangleixingMapper.insertSelective(add);
        if (count == 0) {
            resultVO = ResultVO.fail();
        }
        return resultVO;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO modify(MeetingTypeModifyDTO modifyQueryDTO) {
        ResultVO resultVO = ResultVO.success();


        YwHuichangleixing modify = new YwHuichangleixing();
        BeanUtils.copyProperties(modifyQueryDTO, modify);
        modify.setGengxinrenId(manageUserCache.getCurrentUserId());
        modify.setGengxinshijian(new Date());
        int count = ywHuichangleixingMapper.updateByPrimaryKeySelective(modify);
        if (count == 0) {
            resultVO = ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO delete(MeetingTypeDelDTO delQueryDTO) {
        ResultVO resultVO = ResultVO.success();


        YwHuichangleixing delete = new YwHuichangleixing();
        delete.setId(delQueryDTO.getId());
        delete.setShanchubiaoshi(SysDicConstant.DELFLAG_1);
        delete.setGengxinrenId(manageUserCache.getCurrentUserId());
        delete.setGengxinshijian(new Date());
        int count = ywHuichangleixingMapper.updateByPrimaryKeySelective(delete);
        if (count == 0) {
            resultVO = ResultVO.fail();
        }
        return resultVO;
    }

    @Override
    public RowsDataVO<MeetingTypeQueryResultDTO> getPmsMeetingTypeList(MeetingTypeQueryDTO params) {

        RowsDataVO<MeetingTypeQueryResultDTO> dataVO = new RowsDataVO<>();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        if (null != params.getHuichangleixing()) {
            conditions.addCondition("huichangleixing", EConditionOperator.EQULS, params.getHuichangleixing());
        }
        if (StringUtils.isNotBlank(params.getChangxingmingcheng())) {
            conditions.addCondition("changxingmingcheng", EConditionOperator.LIKE, params.getChangxingmingcheng());
        }
        MyExample example = new MyExample(YwHuichangleixing.class, conditions, params.getOrderFields());
        Page<MeetingTypeQueryResultDTO> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwHuichangleixing> list = ywHuichangleixingMapper.selectByExample(example);
        List<MeetingTypeQueryResultDTO> resultList = list.stream().map(n -> {
            MeetingTypeQueryResultDTO dto = new MeetingTypeQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
            dto.setJiedaidengjiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.JIEDAIDENGJI, dto.getJiedaidengji()));
            dto.setHuichangleixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.HUICHANGLEIXING, dto.getHuichangleixing()));
            return dto;
        }).collect(Collectors.toList());
        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(resultList);
        return dataVO;
    }

    @Override
    public MeetingTypeDetailQueryResultDTO getDetail(MeetingTypeDetailQueryDTO detailQueryDTO) {
        YwHuichangleixing getDetail = new YwHuichangleixing();
        getDetail.setId(detailQueryDTO.getId());
        YwHuichangleixing ywHuichangleixing = ywHuichangleixingMapper.selectByPrimaryKey(getDetail);
        if (null == ywHuichangleixing) {
            return null;
        }

        MeetingTypeDetailQueryResultDTO dto = new MeetingTypeDetailQueryResultDTO();
        BeanUtils.copyProperties(ywHuichangleixing, dto);
        dto.setZhuangtaiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.ZHUANGTAI, dto.getZhuangtai()));
        dto.setJiedaidengjiMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.JIEDAIDENGJI, dto.getJiedaidengji()));
        dto.setHuichangleixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.HUICHANGLEIXING, dto.getHuichangleixing()));
        return dto;
    }


    @Override
    public ResultVO<RowsDataVO<ReservationMeetingTypeResultDTO>> getReservationMeetingTypeList(ReservationMeetingTypeQueryDTO params) {
        ResultVO<RowsDataVO<ReservationMeetingTypeResultDTO>> resultVO = ResultVO.success();
        RowsDataVO<ReservationMeetingTypeResultDTO> dataVO = new RowsDataVO<>();

        Page page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<ReservationMeetingTypeResultDTO> list = ywHuichangleixingMapper.selectReservationMeetingTypeList(params);
        list.forEach(dto -> dto.setHuichangleixingMingCheng(dictInfoCacheExtractor.getDictValue(SysDicConstant.HUICHANGLEIXING, dto.getHuichangleixing())));

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(list);
        resultVO.setData(dataVO);
        return resultVO;
    }


}
